<script setup lang="ts">
import { ElRow, ElCol, ElSkeleton, ElCard, ElLink, ElForm, ElFormItem, ElInput } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { ref, reactive } from 'vue'
import { Search } from '@/components/Search'
import { formatTime } from '@/utils'
import { useValidator } from '@/hooks/web/useValidator'
import { getProjectApi } from '@/api/dashboard/workplace'
import type { Project } from '@/api/dashboard/workplace/types'
import { FormSchema } from '@/types/form'

const { required } = useValidator()
const loading = ref(true)

let projects = reactive<Project[]>([])

// 获取项目数
const getProject = async () => {
  const res = await getProjectApi().catch(() => {})
  if (res) {
    projects = Object.assign(projects, res.data)
  }
}
const { t } = useI18n()

const schema = reactive<FormSchema[]>([
  {
    field: 'link',
    label: '链接名称',
    component: 'Input'
  }
])

const isGrid = ref(false)
const layout = ref('inline')
const buttomPosition = ref('left')
const onSubmit = () => {
  console.log('submit!')
}

const getAllApi = async () => {
  await Promise.all([getProject()])
  loading.value = false
}
const search = (formData) => {
  console.log('submit： ' + JSON.stringify(formData))
}

getAllApi()
</script>

<template>
  <ElRow class="mt-20px" :gutter="20" justify="space-between">
    <ElCol :xl="24" :lg="24" :md="24" :sm="24" :xs="24" class="mb-20px">
      <ElCard shadow="never">
        <Search
          @search="search"
          :schema="schema"
          :is-col="isGrid"
          :layout="layout"
          :buttom-position="buttomPosition"
          :showSearch="true"
          :expand="false"
        />
      </ElCard>
      <ElCard shadow="never" class="mt-20px">
        <template #header>
          <div class="flex justify-between">
            <div>
              <span>我的常用链接</span>
            </div>
            <ElLink type="primary" :underline="false">{{ t('workplace.more') }}</ElLink>
          </div>
        </template>
        <ElSkeleton :loading="loading" animated>
          <ElRow>
            <ElCol
              v-for="(item, index) in projects"
              :key="`card-${index}`"
              :xl="4"
              :lg="4"
              :md="12"
              :sm="24"
              :xs="24"
            >
              <ElCard shadow="hover">
                <div class="flex items-center">
                  <Icon :icon="item.icon" :size="25" class="mr-10px" />
                  <span class="text-16px">{{ item.name }}</span>
                </div>
                <div class="mt-15px text-14px text-gray-400">{{ t(item.message) }}</div>
                <div class="mt-20px text-12px text-gray-400 flex justify-between">
                  <span>{{ item.personal }}</span>
                  <span>{{ formatTime(item.time, 'yyyy-MM-dd') }}</span>
                </div>
              </ElCard>
            </ElCol>
          </ElRow>
        </ElSkeleton>
      </ElCard>
    </ElCol>
  </ElRow>
</template>
